<template>
  <div>
    <nav-top Licon="back" @callback="goBack" class="nav-top"/>

    <div class="add-some gl-content bsb">
      <div class="content">
        <div class="item flex" v-for="(item, i) in formList" :key="i" :class="item.type">
          <div class="dib vat label wsnw">
            <span class="label-inner dib vat taj" :class="type">{{item.label}}</span>:
          </div>
          <template v-if="item.type === 'sch'">
            <div class="school-sch flex-1">
              <div @click="picker(item)" class="picker-show por box df-inner">
                {{item.value || ''}}
                <i class="arrow abs-m"></i>
              </div>
              <input
                      type="text"
                      v-model="item.value"
                      class="gl-input box df-inner w100 bsb"
                      placeholder="手动添加"
              />
            </div>
          </template>
          <template v-else-if="item.type === 't-time'">
            <div class="t-time flex-1">
              <input
                      type="text"
                      :class="{disabled: item.active}"
                      :disabled="item.active"
                      v-model="item.value"
                      class="gl-input box df-inner w100 bsb"
              />
              <span class="day">天</span>
              <div class="radios flex bsb">
                <div
                        class="radio por"
                        @click="item.active = !item.active,item.active? item.value='':item.value"
                >
                  <span class="dib vam circle bsb" :class="{active: item.active}"></span>长期有效
                </div>
              </div>
            </div>
          </template>
          <template v-else-if="item.type === 's-time'">
            <div class="t-time flex-1">
              <input type="text"
                     :class="{disabled: item.active}"
                     :disabled="item.active"
                     v-model="item.value"
                     class="gl-input box df-inner w100 bsb s-time"/>
              <span class="day">小时</span>
            </div>
          </template>
          <template v-else-if="item.options">
            <div @click="picker(item)" class="picker-show por box df-inner">
              {{(item.value || '') | textFormat(item.label)}}
              <i class="arrow abs-m"></i>
            </div>
          </template>
          <template v-else-if="item.radio">
            <div class="radios flex bsb">
              <div
                      class="radio por unselect"
                      v-for="(r, k) in item.radio"
                      :key="r.value"
                      @click="item.value = r.value"
              >
                <span class="dib vam circle bsb" :class="{active: item.value === r.value}"></span>
                {{r.label}}
              </div>
            </div>
          </template>
          <template v-else>
            <input
                    type="text"
                    v-model="item.value"
                    class="gl-input box df-inner"
                    :placeholder="item.placeholder"
            />
          </template>
        </div>
        <div @click="submit" class="btn tac mlr-auto">提交</div>
      </div>
    </div>
  </div>
</template>

<script>
  import {mapGetters} from "vuex";

  export default {
    name: "AddSome",
    computed: {
      ...mapGetters(["proxy_id", "proxy_type", 'userInfo'])
    },
    data() {
      return {
        parent_id: "",
        formList: [],
        forms: {
          stu: {
            title: "添加学生",
            list: [
              {label: "年级", options: [], value: "", prop: ""},
              {label: "班级", options: [], value: "", prop: ""},
              {label: "姓名", value: "", prop: ""},
              {
                label: "性别",
                radio: [{label: "男", value: 1}, {label: "女", value: 2}],
                value: 1,
                prop: ""
              },
              {
                label: "帐号",
                value: "",
                prop: "",
                placeholder: "已有账号，非必填"
              }
            ]
          },
          tea: {
            title: "添加老师",
            list: [
              {label: "姓名", value: "", prop: ""},
              {
                label: "性别",
                radio: [{label: "男", value: 1}, {label: "女", value: 0}],
                value: 1,
                prop: ""
              },
              {label: "学科", options: [], value: "", prop: ""},
              {label: "职称", options: [], value: "", prop: ""}
            ]
          },
          cla: {
            title: "添加班级",
            list: [
              {label: "选择年级", options: [], value: "", prop: ""},
              {label: "班名", value: "", prop: ""}
            ]
          },
          sch: {
            title: "申请开通学校",
            list: [
              {label: "选择地区", options: [], value: "", prop: ""},
              {
                label: "选择学校",
                options: [{name: "请先选择地区"}],
                type: "sch",
                value: "",
                prop: ""
              },
              {label: "管理员帐号", value: "", prop: ""},
              {label: "区域负责人", options: [], value: "", prop: ""},
              {label: "老师帐号数", value: "", prop: ""},
              {label: "学生帐号数", value: "", prop: ""},
              {label: "学生试用时长", type: 's-time', value: "", prop: ""},
              {
                label: "老师试用时长",
                type: "t-time",
                value: "",
                prop: "",
                active: false
              },
              {label: "备注", value: "长期有效", prop: ""}
            ]
          }
        },
        type: this.$route.params.type,
        first: [],
        area: [[], [], []],
        second: [],
        three: [],
        proId: "",
        cityId: "",
        areaId: "",
        areaList: [],
        nianjiList: [],
        nianjiId: "",
        classId: "",
        teamId: ""
      };
    },
    watch: {},
    filters: {
      textFormat(val, label) {
        if (label === "选择地区" && Array.isArray(val)) {
          return val.map(e => e.name).join(" / ");
        }
        return val;
      }
    },
    created() {
      this.initForm();
      // console.log(this.$route.params);
    },
    methods: {
      // 返回
      goBack(val) {
        if (val === "back") {
          this.$router.go(-1);
        }
      },
      async submit() {
        try {
          switch (this.type) {
            case "sch":
              await this.Http.addSchool({
                school_id: this.school_id,
                username: this.forms.sch.list[2].value,
                // num: this.forms.sch.list[4].value,
                hour: this.forms.sch.list[6].value,
                student_num: this.forms.sch.list[5].value,
                student_hour: this.forms.sch.list[6].value,
                proxy_id: this.teamId,
                teacher_hour: this.forms.sch.list[7].active
                  ? 0
                  : this.forms.sch.list[7].value,
                teacher_num: this.forms.sch.list[4].value,
                remark: this.forms.sch.list[8].value
              });
              break;
            case "stu":
              await this.Http.addStudent({
                realname: this.forms.stu.list[2].value,
                username: this.forms.stu.list[4].value,
                sex: this.forms.stu.list[3].value,
                class_id: this.forms.stu.list[1].prop,
                nianji_id: this.forms.stu.list[0].prop
              });
              break;
            case "tea":
              await this.Http.addTeacher({
                realname: this.forms.tea.list[0].value,
                sex: this.forms.tea.list[1].value,
                xueke_id: this.forms.tea.list[2].prop,
                job_title_id: this.forms.tea.list[3].prop,
                school_id: this.$route.params.sid
              });
              break;
            case "cla":
              await this.Http.addClass({
                school_id: this.$route.params.sid,
                name: this.forms.cla.list[1].value,
                nianji_id: this.forms.cla.list[0].prop
              });
              break;
          }
          this.$toast("添加成功");
          this.$router.go(-1);
        } catch (e) {
          this.$err(e);
        }
      },
      getGradeSubject() {
        this.Http.getGradeSubject()
          .then(r => {
            // console.log(r);
            r.list.map(i => (i.name = i.title));
            this.forms.tea.list[2].options = r.list;
            // console.log(this.forms.tea.list[2].options);
          })
          .catch(console.log);
      },
      getJobTitle() {
        this.Http.JobTitle()
          .then(r => {
            // console.log(r);
            this.forms.tea.list[3].options = r.list;
          })
          .catch(console.log);
      },
      getProxyList() {
        if (this.userInfo.proxy_type * 1 === 2) {
          this.forms.sch.list[3].value = this.userInfo.name;
          this.teamId = this.userInfo.proxy_id;
          return;
        }
        this.Http.getProxyList({
          proxy_id: this.proxy_id,
          proxy_type: this.proxy_type,
          all: 1
        })
          .then(res => {
            // console.log(res);
            this.forms.sch.list[3].options = res;
          })
          .catch(console.log);
      },
      getNianjiList(all) {
        if (all) {
          this.Http.getAllGrades().then(res => {
            res.map(e => e.name = e.title);
            this.forms[this.type].list[0].options = [res];
          }).catch(console.log);
          return;
        }
        this.Http.getClassList({school_id: this.$route.params.sid})
          .then(res => {
            // console.log(res);
            let temp = [];
            res.list.map(e => {
              if (!temp.includes(e.nianji_id)) {
                temp.push(e.nianji_id);
                this.nianjiList.push({
                  id: e.nianji_id,
                  name: e.nianji,
                  classList: [{name: e.nianji + e.name, id: e.id}]
                });
              } else {
                this.nianjiList.map(j => {
                  if (j.id == e.nianji_id) {
                    j.classList.push({
                      name: e.nianji + e.name,
                      id: e.id
                    });
                  }
                });
              }
            });
            this.nianjiList = this.nianjiList.sort((a, b) => a.id - b.id);
            this.forms[this.type].list[0].options = this.nianjiList;
          })
          .catch(e => console.log(e));
      },
      getArea(res, i, next) {
        let id = "";
        if (res === "area") {
          id = res;
        } else if (res && res[i]) {
          id = res[i].id;
        }

        if (this.areaList[id] && this.areaList[id].length) {
          let r =
            i === undefined
              ? this.forms.sch.list[0].options[0]
              : this.forms.sch.list[0].options[i];
          if ((i === undefined || (next && next < 2)) && r.length) {
            let p = {};
            i = next || i || 0;
            p[i] = this.forms.sch.list[0].value[i] || r[0];
            this.getArea(p, i, i + 1);
          }
          this.forms.sch.list[0].options.splice(i + 1, 1, this.areaList[id]);
          return;
        }
        if (i >= 2) return;
        this.Http.getArea({parent_id: id}, {loading: false})
          .then(r => {
            let curr = this.forms.sch.list[0];
            this.areaList[id || "area"] = r;
            curr.options.splice(i + 1, 1, r);
            if (!i && !r.length) {
              curr.options.splice(2, 1, []);
            }
            if ((i === undefined || (next && next < 2)) && r.length) {
              let p = {};
              i = next || i || 0;
              p[i] = r[0];
              this.getArea(p, i, i + 1);
            }
          })
          .catch(console.log);
      },
      getAreaSchool(province, city, county_id) {
        this.Http.getAreaSchool({
          province,
          city,
          county_id
        })
          .then(r => {
            // console.log(r);
            this.forms.sch.list[1].options = r;
          })
          .catch(console.log);
      },
      initForm() {
        let item = this.forms[this.type];
        if (item) {
          this.formList = item.list || [];
          document.title = item.title;
          switch (this.type) {
            case "sch":
              this.getArea();
              this.getProxyList();
              break;
            case "stu":
              this.getNianjiList();
              break;
            case "tea":
              this.getGradeSubject();
              this.getJobTitle();
              break;
            case "cla":
              this.getNianjiList(true);
          }
        } else {
          this.$router.go(-1);
        }
      },
      picker(item) {
        // console.log(item);
        let tit = item.label;
        let that = this;
        if (tit === "选择地区") {
          this.getArea("area");
        }

        if (tit === '区域负责人' && this.userInfo.proxy_type * 1 === 2) {
          return;
        }

        this.showPicker({
          options: item.options,
          title: item.label,
          value:
            tit === "选择地区"
              ? item.value
              : item.options.filter(e => e.name === item.value),
          sure(res, fn) {
            // console.log(res);
            // console.log(item);
            item.prop = res[0].id;
            item.value = res[0].name;
            if (that.type === "stu") {
              if (tit === "年级") {
                that.forms.stu.list[1].value = "";
                that.forms.stu.list[1].prop = "";
                that.forms.stu.list[1].options = res[0].classList;
              }
            } else if (that.type === "sch") {
              if (tit === "选择地区") {
                item.value = res;
                that.getAreaSchool(res[0].id, res[1].id, res[2].id);
                that.province = res[0].id;
                that.cityId = res[1].id;
                that.areaId = res[2].id;
              } else if (tit === "选择学校") {
                that.school_id = res[0].id;
              } else {
                that.teamId = res[0].id;
              }
            }
            fn();
          },
          change(res, i) {
            // console.log(res, i);
            if (tit === "选择地区" && i < 2) {
              that.getArea(res, i);
            }
          }
        });
      }
    }
  };
</script>

<style scoped lang="less">
  .add-some {
    padding-top: 0;
    height: calc(100vh - 104px);
  }

  .nav-top {
    background-color: #f7f8fa;
  }

  .content {
    padding: 34px 34px 40px;
    background: #fff;
    box-shadow: 0 6px 18px 0 rgba(0, 0, 0, 0.1);
    border-radius: 20px;
  }

  .item {
    align-items: center;
    margin-bottom: 34px;
    &.sch,
    &.t-time {
      align-items: unset;
      .radio {
        color: #777;
      }
    }
    .school-sch {
      .picker-show {
        margin-bottom: 34px;
      }
    }
    .t-time {
      position: relative;
      .gl-input {
        margin-bottom: 34px;
        &.s-time {
          margin-bottom: 0;
        }
      }
      .day {
        position: absolute;
        right: 16px;
        top: 0;
        height: 21px;
        font-size: 24px;
        font-weight: 400;
        color: rgba(186, 186, 186, 1);
        line-height: 48px;
      }
      .disabled {
        background-color: #ccc;
        opacity: 0.5;
      }
    }
  }

  .label {
    margin-right: 26px;
    font-size: 34px;
    color: #777;
    line-height: 52px;
    .label-inner {
      width: 70px;
      &.cla {
        width: 138px;
      }
      &.sch {
        width: 204px;
      }
    }
  }

  .df-inner {
    padding-left: 19px;
    flex: 1;
    height: 50px;
    line-height: 50px;
    border: 1px solid #ececec;
    border-radius: 6px;
    .arrow {
      right: 18px;
      width: 18px;
      height: 12px;
      background: url(../../../assets/images/icon/class_upArrow.png) no-repeat left top / 100% 100%;
    }
  }

  .radios {
    padding: 4px 0 0;
    border: 0;
    font-size: 34px;
    line-height: 1;
    color: #222;
    .radio {
      margin-right: 42px;
    }
    .circle {
      margin-right: 13px;
      vertical-align: middle;
      padding: 4px;
      width: 30px;
      height: 30px;
      border: 1px solid #999;
      border-radius: 50%;
      box-sizing: border-box;
      &:after {
        content: "";
        transition: background-color 0.3s;
        display: block;
        height: 100%;
        border-radius: 50%;
        box-sizing: border-box;
        // vertical-align: top;
        background-color: transparent;
        // position: absolute;
        // left: 0;
        // top: 0;
        // transform: translate(50%, 50%);
        // margin:0px 0 2px 3px;
      }
    }
    .active {
      border-color: #14c8ff;
      &:after {
        background-color: #14c8ff;
      }
    }
  }

  .btn {
    margin-top: 40px;
    width: 290px;
    height: 90px;
    line-height: 90px;
    font-size: 32px;
    color: #fff;
    background: linear-gradient(-83deg, rgb(20, 200, 255), rgb(2, 151, 255));
    box-shadow: 0 7px 12px 0 rgba(79, 184, 254, 0.4);
    border-radius: 20px;
  }
</style>
